package com.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.entity.Cake;
import com.entity.ConditionCakeType;
import com.google.gson.Gson;
import com.servers.CakeServers;

@Controller
public class SearchController {
	@Autowired
	private CakeServers cakeServers;
	
	@RequestMapping("/goSearchJsp")
	public ModelAndView goSearchJsp(String title,ModelMap modelMap) {
		ModelAndView modelAndView=new ModelAndView();
		System.out.println("--------------进入到模糊查询的界面------------------"+title);
		List<Cake> querySearchAllCake = cakeServers.QuerySearchAllCake("%"+title+"%");
		//System.out.println(querySearchAllCake+"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		List<Map<String, Object>> list=new ArrayList<>();
		if (querySearchAllCake.size()!=0) {
			System.out.println("------------------进入if1------------------------");
			
			for (Cake cake : querySearchAllCake) {
				//System.out.println(cake);
				System.out.println("------------------进入if2222-----------------------");
				Map<String, Object> tempmap = new HashMap<>();
				tempmap.put("img","images/Fruits/"+cake.getCakeName()+".png");// 图片路径
				tempmap.put("title",cake.getCakeName() );//图片标题. name
				tempmap.put("ciurPic",cake.getCakePrice());//现价
				tempmap.put("OriPic",cake.getCakePrice()*2 );//原价
				tempmap.put("discount", "5折");//打折 折扣
				list.add(tempmap);
			}
			modelAndView.addObject("searchlist", list);
			modelAndView.addObject("success", "images/img/scsearch.png");
			
		}else {
			System.out.println("------------------进入else------------------------");
			ConditionCakeType conditionCakeType=new ConditionCakeType();
			conditionCakeType.setCakeTypeId(8);
			conditionCakeType.setSort("desc");
			conditionCakeType.setType("cakesales");
			List<Cake> cakeByTypeId = cakeServers.getCakeByTypeId(conditionCakeType);
			for (int i = 0; i < 12; i++) {
				Cake cake=new Cake();
				cake = cakeByTypeId.get(i);
				Map<String, Object> tempmap = new HashMap<>();
				tempmap.put("img","images/Fruits/"+cake.getCakeName()+".png");// 图片路径
				tempmap.put("title",cake.getCakeName() );//图片标题. name
				tempmap.put("ciurPic",cake.getCakePrice());//现价
				tempmap.put("OriPic",cake.getCakePrice()*2 );//原价
				tempmap.put("disCount", "5折");//打折 折扣
				list.add(tempmap);
			}
			//map.put("searchlist", list);
			modelAndView.addObject("success","images/img/QQ1111.png" );
			modelAndView.addObject("searchlist", list);
		}
		ConditionCakeType conditionCakeType3=new ConditionCakeType(9, "cakesales", "desc");
		Map<String, Object> getgoBuytodayJson3 = cakeServers.getgoBuytodayJson(conditionCakeType3, "footerList",null);
		modelAndView.addObject("footer", getgoBuytodayJson3);
		modelAndView.setViewName("search");
		//modelMap.put("adsa", "hello");
		System.out.println(list.size()+"~~~~~~~~~~~~~查找出的长度的数值~~~~~~~~~~~~~~~~~~~~~");
		//System.out.println(map+"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
		System.out.println(modelAndView.getModelMap());
		
		return modelAndView;
	}

	public CakeServers getCakeServers() {
		return cakeServers;
	}

	public void setCakeServers(CakeServers cakeServers) {
		this.cakeServers = cakeServers;
	}
	
}
